﻿using System;
using System.IO;

namespace Orange.Lib.Effective
{
    namespace DisposeUse
    {
        class FileUse
        {
            private string filePath = @"d:\1.txgt";

            public void ReadFile()
            {
                using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
                using (StreamReader sr = new StreamReader(fs))
                {
                    string line = null;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
        }

        class MyResourceHog : IDisposable
        {
            private bool alreadyDisposed = false;

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

            protected virtual void Dispose(bool isDisposing)
            {
                if (alreadyDisposed)
                {
                    return;
                }
                if (isDisposing)
                {
                    //free managed resources here.
                }

                //free unmanaged resources.
                alreadyDisposed = true;
            }

            public void ExampleMethod()
            {
                if (alreadyDisposed)
                {
                    throw new ObjectDisposedException(
                        "MyResourceHog",
                        "Called Example Method on Disposed object.");
                }
            }
        }

        class A
        {
            private int a;
            public A()
            {
                a = 1;
            }
        }

        class B : A
        {
            private int a;
            public B()
            {
                //a = 2;
            }

            public int GetA()
            {
                return a;
            }
        }

    }
}
